<?php

use Phinx\Migration\AbstractMigration;

class SyncGateWayTableChanges extends AbstractMigration
{

    public function change()
    {
        $this->table("oauth_client")->changeColumn("scope", "string", [
            "default" => "read,write",
            'null' => false,
            'limit' => 4000,
            'comment' => "指定客户端申请的权限范围,可选值包括read,write,trust;若有多个权限范围用逗号(,)分隔,如: read,write"
        ])->save();

        $this->table("oauth_code")
            ->removeColumn("device_unique_code")
            ->removeColumn("uuid")
            ->removeColumn("type")
            ->addColumn("id_token", "string", [
                "default" => "",
                "comment" => "id_token",
                "null" => false,
                "limit" => 1000,
                "after" => "code",
            ])->changeColumn("user_id", "integer", [
                "comment" => "用户id",
                "null" => false,
                "limit" => 11,
                "default" => 0,
                "after" => "id_token"
            ])
            ->changeColumn("redirect_uri", "string", [
                "null" => false,
                "default" => "",
                "comment" => "回调地址",
                "limit" => 2048,
                "after" => "user_id",
            ])
            ->changeColumn("scope", "string", [
                "limit" => 4000,
                "null" => false,
                "default" => "read,write",
                "comment" => "指定客户端申请的权限范围,可选值包括read,write,trust;若有多个权限范围用逗号(,)分隔,如: read,write"
            ])
            ->save();

        $this->table("oauth_token")
            ->removeColumn("refresh_token")
            ->removeColumn("device_unique_code")
            ->removeColumn("ip")
            ->removeColumn("uuid")
            ->addColumn("app_key", "string", [
                'default' => "",
                "null" => false,
                "comment" => "唯一标识每一个客户端(client),后端自动生成",
                "limit" => 36,
                "after" => "user_id",
            ])
            ->changeColumn("scope", "string", [
                "default" => "read,write",
                "null" => false,
                "limit" => 4000,
                "comment" => "指定客户端申请的权限范围,可选值包括read,write,trust;若有多个权限范围用逗号(,)分隔,如: read,write"
            ])
            ->save();


        $this->table("user")
            ->changeColumn("scope", "string", [
                "limit" => 4000,
                "default" => "",
                "comment" => "范围",
                "null" => false
            ])
            ->save();

        $this->table("oauth_device", ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '设备表'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => 'Oauth 信任设备自增主键'])
            ->addColumn("user_id", "integer", [
                "limit" => 11,
                "null" => false,
                "default" => 0,
                "comment" => "用户id",
                "signed" => false,
            ])
            ->addColumn("device_unique_code", "string", [
                "limit" => 36,
                "null" => false,
                "default" => "",
                "comment" => "设备唯一码",
            ])
            ->addColumn("ip", "string", [
                "limit" => 32,
                "default" => "",
                "null" => false,
                "comment" => "IP地址"
            ])
            ->addColumn("created", "integer", [
                "limit" => 11,
                'default' => 0,
                'null' => false,
                "comment" => "创建时间",
                "signed" => false,
            ])->save();

        $this->table("oauth_refresh_token", ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '设备表'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => 'Oauth刷新验证码ID'])
            ->addColumn("user_id", "integer", [
                "limit" => 11,
                "null" => false,
                "default" => 0,
                "comment" => "用户id",
                "signed" => false,
            ])
            ->addColumn("app_key", "string", [
                'default' => "",
                "null" => false,
                "comment" => "唯一标识每一个客户端(client),后端自动生成",
                "limit" => 36,
            ])
            ->addColumn("refresh_token", "string", [
                "limit" => 40,
                "default" => "",
                "null" => false,
                "comment" => "刷新令牌",
            ])
            ->addColumn("expires", "integer", [
                "null" => false,
                "limit" => 11,
                "default" => 0,
                "comment" => "失效时间",
                "signed" => false,

            ])
            ->addColumn("scope", "string", [
                "default" => "read,write",
                "null" => false,
                "limit" => 4000,
                "comment" => "指定客户端申请的权限范围,可选值包括read,write,trust;若有多个权限范围用逗号(,)分隔,如: read,write"
            ])
            ->addColumn("created", "integer", [
                "limit" => 11,
                'default' => 0,
                'null' => false,
                "comment" => "创建时间",
                "signed" => false,
            ])
            ->save();
    }
}
